home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / SAT 2.3.8 / Demos / StepPlatform Demo ƒ / sMovPlatform.p < prev    next >
Text File  |  1995-09-03  |  3KB  |  115 lines

  1. (* Platform sprite, moveable version, not faceless *)
  2. (* *)
  3.  
  4. unit sMovPlatform;
  5. interface
  6.     uses
  7. {$ifc UNDEFINED THINK_PASCAL}
  8.         Types, QuickDraw, Menus, Windows, TextEdit, Fonts, Dialogs, Memory, {}
  9. {$endc}
  10.         SAT, PlatformGlobals, sPlayerSprite;
  11.  
  12.     procedure InitMovPlatform;
  13.     procedure SetupMovPlatform (me: SpritePtr);
  14.  
  15. implementation
  16.  
  17.     const
  18.         MovPlatformSpeed = 2;
  19.  
  20.     var
  21.         platFace: FacePtr;
  22.  
  23.     procedure InitMovPlatform;
  24.     begin
  25.         platFace := SATGetFace(139);
  26.     end;
  27.  
  28.     procedure HandleMovPlatform (me: SpritePtr);
  29.     begin
  30.         me^.position.v := me^.position.v + me^.speed.v;
  31.         if (me^.position.v < MovPlatP(me^.appPtr)^.MinV) then
  32.             me^.speed.v := MovPlatformSpeed;
  33.         if (me^.position.v > MovPlatP(me^.appPtr)^.MaxV) then
  34.             me^.speed.v := -MovPlatformSpeed;
  35.  
  36.         if (me^.speed.v = 0) then
  37.             begin
  38.                 if (me^.position.v > (MovPlatP(me^.appPtr)^.MaxV - MovPlatP(me^.appPtr)^.MinV) div 2) then
  39.                     me^.speed.v := -MovPlatformSpeed
  40.                 else
  41.                     me^.speed.v := MovPlatformSpeed;
  42.             end;
  43.  
  44.         me^.layer := -me^.position.v;
  45.     end;
  46.  
  47.     procedure HitMovPlatform (me: SpritePtr; him: PlSpritePtr);
  48.         var
  49.             mini, i, min: Integer;
  50.             diff: array[0..5] of Integer;
  51.     begin
  52.         if (him^.task = @HandlePlayerSprite) then
  53.             begin
  54.                 diff[1] := -me^.hotRect2.top + (him^.hotRect2.bottom);        (* (T)toB *)
  55.                 diff[2] := -him^.hotRect2.top + (me^.hotRect2.bottom);        (* (B)toT *)
  56.                 diff[3] := -me^.hotRect2.left + (him^.hotRect2.right);            (* LtoR *)
  57.                 diff[4] := -him^.hotRect2.left + (me^.hotRect2.right);            (* RtoL *)
  58.                 mini := 0;
  59.                 min := 10000;
  60.                 for i := 1 to 4 do
  61.                     if min > diff[i] then
  62.                         begin
  63.                             min := diff[i];
  64.                             mini := i;
  65.                         end;
  66.                 case mini of
  67.                     1: (*floor*)
  68.                         begin
  69.                             him^.action := Stand;
  70.                             him^.position.v := him^.position.v - diff[1] + 2;
  71.                             if (him^.speed.v > 0) then
  72.                                 him^.speed.v := 0;
  73.                             him^.speed.h := 0;
  74.                         end;
  75.                     2: (* ceiling *)
  76.                         begin
  77.                             him^.position.v := him^.position.v + diff[2] + 1;
  78.                             if (him^.speed.v < 0) then
  79.                                 him^.speed.v := -him^.speed.v;
  80.                         end;
  81.                     3: (*left*)
  82.                         begin
  83.                             him^.position.h := him^.position.h - diff[3] - 1;
  84.                             if (him^.speed.h > 0) then
  85.                                 him^.speed.h := -him^.speed.h;
  86.                         end;
  87.                     4: (*right*)
  88.                         begin
  89.                             him^.position.h := him^.position.h + diff[4] + 1;
  90.                             if (him^.speed.h < 0) then
  91.                                 him^.speed.h := -him^.speed.h;
  92.                         end;
  93.                 end; (* switch *)
  94.             end;  (* if *)
  95.     end;
  96.  
  97.     procedure SetupMovPlatform (me: SpritePtr);
  98.         var
  99.             r: Rect;
  100.             pol: PolyHandle;
  101.     begin
  102.         me^.speed.v := -MovPlatformSpeed + SATRand(2) * 2;
  103.         me^.face := platFace;
  104.         me^.appPtr := NewPtr(sizeof(MovPlatRec));
  105.         if me^.appPtr <> nil then
  106.             begin
  107.                 MovPlatP(me^.appPtr)^.MaxV := me^.position.v;
  108.                 MovPlatP(me^.appPtr)^.MinV := me^.kind;
  109.             end;
  110.         SetRect(me^.hotRect, 0, 3, 60, 20);
  111.         me^.task := @HandleMovPlatform;
  112.         me^.hitTask := @HitMovPlatform;
  113.     end;
  114.  
  115. end.